package com.papper.common.service.base;

import com.papper.common.bean.base.BaseMqEvent;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.local.GuavaCacheFactory;
import com.papper.common.cache.redis.CacheClosureUnDefault;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.mapper.base.BaseMqEventMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:任务执行事件
 */
@Service("baseMqEventService")
public class BaseMqEventService extends ServiceImpl<BaseMqEventMapper, BaseMqEvent> {
    private static final String LocalMemKey = "mqEventServiceMemKey";

    @Override
    protected void clearCaches(BaseMqEvent entity) {
        super.clearCaches(entity);
        cache.delete(CacheConstants.Keys(getModel(), entity.getMgEvent()));
    }

    /**
     * function
     */
    public BaseMqEvent findByEvent(final String mqEvent) {
        if (StringTool.isEmpty(mqEvent)) {
            return null;
        }
        return cache.getAndSet(new CacheClosureUnDefault<BaseMqEvent>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), mqEvent);
            }

            @Override
            public BaseMqEvent getValue() {
                Example          example  = new Example(BaseMqEvent.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("mgEvent", mqEvent);
                return findOne(example);
            }

            @Override
            public Class<BaseMqEvent> getTypeReference() {
                return BaseMqEvent.class;
            }
        });
    }

    public BaseMqEvent createEvent(String mqEvent) {
        //MEM-DB
        BaseMqEvent memData = (BaseMqEvent) GuavaCacheFactory.instance().get(LocalMemKey, mqEvent);
        if (memData != null) {
            return memData;
        }
        //REDIS-DB
        BaseMqEvent dbEvent = findByEvent(mqEvent);
        if (dbEvent != null) {
            GuavaCacheFactory.instance().put(LocalMemKey, mqEvent, dbEvent);
            return dbEvent;
        }
        //Mysql-DB
        dbEvent = new BaseMqEvent();
        dbEvent.setMgEvent(mqEvent);
        dbEvent.setMqName(mqEvent);
        dbEvent.setMqGroup(1);
        dbEvent.setDataStatus(DataStatusEnum.OPEN.getCode());
        dbEvent.setCreateTime(new Date());
        dbEvent.setModifyTime(new Date());
        save(dbEvent);
        GuavaCacheFactory.instance().put(LocalMemKey, mqEvent, dbEvent);
        return dbEvent;
    }

    public List<BaseMqEvent> findByGroup(Integer mqGroup) {
        Example          example  = new Example(BaseMqEvent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mqGroup", mqGroup);
        return find(example);
    }
}
